﻿//===============================================================================
// File: FileHelper.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.1
// Created Date:  2011/3/6 8:24:03 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPMS.BLL
{
    using GPMS.Model;
    using Webdiyer.WebControls.Mvc;
    public static class FileHelper
    {
        public static File Get(Guid id,ExtendedGPMSDb db)
        {
            return db.Files.FirstOrDefault(f => f.FileID == id);
        }

        public static List<File> GetList(FileType type, ExtendedGPMSDb db)
        {
            string fileType = type.ToString();
            return db.Files.Where(f => f.FileType == fileType).ToList();
        }

        public static Guid Add(File file, ExtendedGPMSDb db)
        {
            if (file.FileID==Guid.Empty)
            {
                file.FileID = Guid.NewGuid();
            }
            file.CreateDate = DateTime.Now;
            db.Files.AddObject(file);
            db.SaveChanges();
            return file.FileID;
        }

        public static PagedList<File> GetUserFileList(Guid userID, int page, ExtendedGPMSDb db)
        {
            string type=FileType.UserFile.ToString();
            return db.Files
                .Where(f => f.UserInfo.UserID == userID && f.FileType == type)
                .OrderByDescending(f => f.CreateDate)
                .ToPagedList(page, SettingHelper.Get(db).PageSize);
        }

        public static List<File> GetUserFileList(Guid userID, ExtendedGPMSDb db)
        {
            string type = FileType.UserFile.ToString();
            return db.Files
                .Where(f => f.UserInfo.UserID == userID && f.FileType == type)
                .OrderByDescending(f => f.CreateDate).ToList();
        }
    }
}
